<?php

namespace App\Models\Plane\Comfort;

use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * App\Models\Plane\Comfort\PlaneComfortCabins
 *
 * @property int $id
 * @property string $flight_no 航班号
 * @property string $cabin 舱位
 * @property string $seat_space 位置
 * @property string $seat_width 宽度
 * @property string $seat_tilt 倾斜度
 * @property string $meal 有无餐食
 * @property string $meal_type 餐食类型
 * @property string $has_media 有无多媒体
 * @property string $media 多媒体
 * @property string $has_power 有无电源
 * @property string $power 电源类型
 * @property string $has_wifi 有无wifi
 * @property string $wifi wifi
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @method static Builder|PlaneComfortCabins newModelQuery()
 * @method static Builder|PlaneComfortCabins newQuery()
 * @method static Builder|PlaneComfortCabins query()
 * @method static Builder|PlaneComfortCabins whereCabin($value)
 * @method static Builder|PlaneComfortCabins whereCreatedAt($value)
 * @method static Builder|PlaneComfortCabins whereFlightNo($value)
 * @method static Builder|PlaneComfortCabins whereHasMedia($value)
 * @method static Builder|PlaneComfortCabins whereHasPower($value)
 * @method static Builder|PlaneComfortCabins whereHasWifi($value)
 * @method static Builder|PlaneComfortCabins whereId($value)
 * @method static Builder|PlaneComfortCabins whereMeal($value)
 * @method static Builder|PlaneComfortCabins whereMealType($value)
 * @method static Builder|PlaneComfortCabins whereMedia($value)
 * @method static Builder|PlaneComfortCabins wherePower($value)
 * @method static Builder|PlaneComfortCabins whereSeatSpace($value)
 * @method static Builder|PlaneComfortCabins whereSeatTilt($value)
 * @method static Builder|PlaneComfortCabins whereSeatWidth($value)
 * @method static Builder|PlaneComfortCabins whereUpdatedAt($value)
 * @method static Builder|PlaneComfortCabins whereWifi($value)
 * @mixin Eloquent
 */
class PlaneComfortCabins extends Model
{
    use HasFactory;

    protected $table = 'plane_comfort_cabins';

    protected $fillable
        = [
            'flight_no',
            'cabin',
            'seat_space',
            'seat_width',
            'seat_tilt',
            'meal',
            'meal_type',
            'has_media',
            'media',
            'has_power',
            'power',
            'has_wifi',
            'wifi',
        ];

    public function add(PlaneComfort $comfort, array $data)
    {
        $data['flight_no'] = $comfort->flight_no;
        if ($cabin = $this->findByFlightAndCabin($data['flight_no'], $data['cabin'])) {
            // 如果存在差异则更新
            $update = collect($data)->only($this->fillable);
            if (collect($cabin)->only($this->fillable)->diff($update)->count()) {
                $cabin->update($update->toArray());
                $ck = "comfort:cabin:{$data['flight_no']}:{$data['cabin']}";
                Cache::put($ck, $cabin, config('trip.ttl.month'));
            }
            return $cabin;
        }
        return $this->create($data);
    }

    public function findByFlightAndCabin(string $flight_no, string $cabin)
    {
        $ck = "comfort:cabin:{$flight_no}:{$cabin}";
        if (Cache::has($ck)) {
            return Cache::get($ck);
        }

        $cabin = $this->where([
            'flight_no' => $flight_no,
            'cabin'     => $cabin,
        ])->first();
        $cabin && Cache::put($ck, $cabin, config('trip.ttl.month'));
        return $cabin;


    }
}
